<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Schedule_model extends CI_Model {
    
    public $tablename = 'hpg_schedule';
    
    public $project_follows_tab = 'hpg_project_follows';
    
    public function __construct()
    {
        parent::__construct();
    }
    
    public function getDetail($id){
        if(!$id){
            return false;
        }
        $data = $this->db->select('id,title,project_id,start,end,location,description')->from($this->tablename)->where('id', $id)->limit(1)->fetchOne();
        return $data ? $data : false;
    }
    
    public function getList( $uid, $project_id, $month, $all=false ){
        $month_stime = strtotime($month);
        $month_etime = strtotime(date('Ym01', $month_stime).'+1 month');
        $map = '((start>'.$month_stime.' and start<'.$month_etime.') or (end>'.$month_stime.' and end<'.$month_etime.'))';
        if( $project_id ){
            $map .= ' AND b.project_id='.$project_id;
        }
        if( !$all ){
            $map .= ' AND a.uid='.$uid;
        }
        $list = $this->db->select('b.id,b.title,b.project_id,b.start,b.end,b.location,b.description,a.color,a.color_type')->from($this->tablename.' as b')
        ->join($this->project_follows_tab.' as a', 'b.project_id='.'a.project_id')
        ->where($map)
        ->get_array();
        //->_compile_select();exit;
        return $list ? $list : [];
    }
    
    public function insert_db($data){
        if( !$data ){
            return false;
        }
        $res = $this->db->insert($this->tablename, $data);
        return $this->db->insert_id();
    }
    
    public function update($map, $data){
        if( !$map || !$data ){
            return false;
        }
        $res = $this->db->update($this->tablename, $data, $map);
        return $res;
    }
    
    public function delete($id){
        if( !$id ){
            return false;
        }
        $res = $this->db->delete($this->tablename, ['id'=>$id]);
        return $res;
    }
}
?>